<?php
class MY_Pagination extends CI_Pagination
{
	var $of_tag_open	= '<span id="of">';
	var $of_tag_close	= '</span>';
	
	function __construct($params = array())
	{
		return parent::__construct($params);
	}
	
	function create_links($type = 'CI')
	{
		// If our item count or per-page total is zero there is no need to continue.
		if ($this->total_rows == 0 OR $this->per_page == 0)
		{
		   return '';
		}

		// Calculate the total number of pages
		$this->num_pages = ceil($this->total_rows / $this->per_page);

		// Is there only one page? Hm... nothing more to do here then.
		if ($this->num_pages == 1)
		{
			return '';
		}

		// Determine the current page number.		
		$CI =& get_instance();
		
		if ($CI->config->item('enable_query_strings') === TRUE OR $this->page_query_string === TRUE)
		{
			if ($CI->input->get($this->query_string_segment) != 0)
			{
				$this->cur_page = $CI->input->get($this->query_string_segment);
				
				// Prep the current page - no funny business!
				$this->cur_page = (int) $this->cur_page;
			}
		}
		else
		{
			if ($CI->uri->segment($this->uri_segment) != 0)
			{
				$this->cur_page = $CI->uri->segment($this->uri_segment);
				
				// Prep the current page - no funny business!
				$this->cur_page = (int) $this->cur_page;
			}
		}

		$this->num_links = (int)$this->num_links;
		
		if ($this->num_links < 1)
		{
			show_error('Your number of links must be a positive number.');
		}
				
		if ( ! is_numeric($this->cur_page))
		{
			$this->cur_page = 0;
		}
		
		// Is the page number beyond the result range?
		// If so we show the last page
		if ($this->cur_page > $this->total_rows)
		{
			$this->cur_page = ($this->num_pages - 1) * $this->per_page;
		}
		
		$this->uri_page_number = $this->cur_page;
		$this->cur_page = floor(($this->cur_page/$this->per_page) + 1);

		// Calculate the start and end numbers. These determine
		// which number to start and end the digit links with
		$this->start = (($this->cur_page - $this->num_links) > 0) ? $this->cur_page - ($this->num_links) : 1;
		$this->end   = (($this->cur_page + $this->num_links) < $this->num_pages) ? $this->cur_page + $this->num_links : $this->num_pages;

		/*
		 * Custom pagination 
		 */
//		echo $this->start,$this->num_links,$this->cur_page;
		if ($this->start + $this->num_links > $this->cur_page)
		{
			$this->end = $this->end + $this->num_links - ($this->cur_page - $this->start);
			
			if ($this->end > $this->num_pages)
			{
				$this->end = $this->num_pages;
			} 
		}
		elseif ($this->end - $this->num_links < $this->cur_page)
		{
			$this->start = $this->start - $this->num_links + ($this->end - $this->cur_page); 
		}
		
		// Is pagination being used over GET or POST?  If get, add a per_page query
		// string. If post, add a trailing slash to the base URL if needed
		if ($CI->config->item('enable_query_strings') === TRUE OR $this->page_query_string === TRUE)
		{
			if (strpos($this->base_url, '?') === false)
			{
				$this->base_url = rtrim($this->base_url).'?'.$this->query_string_segment.'=';
			}
			else
			{
				$this->base_url = rtrim($this->base_url).'&amp;'.$this->query_string_segment.'=';
			}
		}
		else
		{
			$this->base_url = rtrim($this->base_url, '/') .'/';
		}

  		// And here we go...
		$output = '';

		switch ($type)
		{
			case 'kassandra':
//				$output .= $this->first_link();
				$output .= $this->previous_link();
				$output .= $this->of_tag_open . sprintf('%d of %d', $this->cur_page, $this->num_pages) . $this->of_tag_close;
				$output .= $this->digit_links();
				$output .= $this->next_link();
//				$output .= $this->last_link();
				break;
		

            				
			case 'mc':
				$output .= '<span class="redsimpletext">' . T_('Page') . '</span>&nbsp;';
//				$output .= $this->first_link();
//				$output .= '<td width="20">' . $this->previous_link() . '</td>';
//				$output .= $this->of_tag_open . sprintf('%d of %d', $this->cur_page, $this->num_pages) . $this->of_tag_close;
				$output .= '<span>'. $this->digit_links() . '</span>';
//				$output .= '<td width="20">' . $this->next_link() . '</td>';
//				$output .= $this->last_link();
				$output .= '</span>';
				break;
			
			 default:
				$output .= $this->first_link();
				$output .= $this->previous_link();
				$output .= $this->digit_links();
				$output .= $this->next_link();
				$output .= $this->last_link();
		}

		// Kill double slashes.  Note: Sometimes we can end up with a double slash
		// in the penultimate link so we'll kill all double slashes.
		$output = preg_replace("#([^:])//+#", "\\1/", $output);

		// Add the wrapper HTML if exists
		$output = $this->full_tag_open.$output.$this->full_tag_close;
		
		return $output;		
	}
	
	private function first_link()
	{
		// Render the "First" link
		if  ($this->cur_page > ($this->num_links + 1))
		{
			return $this->first_tag_open.'<a href="'.$this->base_url.'">'.$this->first_link.'</a>'.$this->first_tag_close;
		}
	}
	
	private function previous_link()
	{
		// Render the "previous" link
		if  ($this->cur_page != 1)
		{
			$i = $this->uri_page_number - $this->per_page;
			if ($i == 0) $i = '';
			
			return $this->prev_tag_open.'<a href="'.$this->base_url.$i.'">'.$this->prev_link.'</a>'.$this->prev_tag_close;
		}
	}

	private function digit_links()
	{
		$output = '';

		// Write the digit links
		for ($loop = $this->start; $loop <= $this->end; $loop++)
		{
			$i = ($loop * $this->per_page) - $this->per_page;
					
			if ($i >= 0)
			{
				$span = ($loop != $this->end) ? "<span style='float: right;' > | </span>" : "";
				
				if ($this->cur_page == $loop)
				{
					$output .= $this->cur_tag_open . $loop . $span . $this->cur_tag_close; // Current page
				}
				else
				{
					$n = ($i == 0) ? '' : $i;
					$output .= $this->num_tag_open.'<a href="'.$this->base_url.$n.'">' . $loop . '</a>'. $span . $this->num_tag_close;
				}
			}
		}
		
		return $output;
	}
	
	private function next_link()
	{
		// Render the "next" link
		if ($this->cur_page < $this->num_pages)
		{
			return $this->next_tag_open.'<a href="'.$this->base_url.($this->cur_page * $this->per_page).'">'.$this->next_link.'</a>'.$this->next_tag_close;
		}
	}
	
	private function last_link()
	{
		// Render the "Last" link
		if (($this->cur_page + $this->num_links) < $this->num_pages)
		{
			$i = (($this->num_pages * $this->per_page) - $this->per_page);
			
			return $this->last_tag_open.'<a href="'.$this->base_url.$i.'">'.$this->last_link.'</a>'.$this->last_tag_close;
		}
	}
}
?>
